package com.geticsen.leetcode;
//https://leetcode-cn.com/problems/merge-two-binary-trees/
public class Solution_617 {
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        if (root1 == null) {
            return root2;
        }
        merge(root1, root2);
        return root1;
    }
    public void merge(TreeNode node1, TreeNode node2) {
        if (node1 == null && node2 == null) {
            return;
        }
        //合并
        if (node1 != null && node2 != null) {
            node1.val = node1.val + node2.val;
            merge(node1.left, node2.left);
            merge(node1.right, node2.right);
        }
        //特殊处理

        if (node1 != null && node1.left == null) {
            node1.left = node2 == null ? null : node2.left;
        }
        if (node1 != null && node1.right == null) {
            node1.right = node2 == null ? null : node2.right;
        }
    }

}
